/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.timesoft.base.ctrl;

import com.timesoft.common.bean.ResponseCommon;
import com.timesoft.common.convertor.Base64ConvertorUtils;
import com.timesoft.global.webservice.WebServiceClient;
import com.timesoft.hibernate.bean.Customer;
import com.timesoft.webservice.client.base.BaseWebService;
import com.timesoft.zk.common.LocaleType;
import com.timesoft.zk.common.NavigatorUtil;
import com.timesoft.zk.common.ctrl.IncludePageWsController;
import com.timesoft.zk.common.ctrl.MainPageWsController;
import com.timesoft.zk.common.event.MainEventWsListener;
import org.apache.log4j.Logger;
import org.zkoss.web.Attributes;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Button;
import org.zkoss.zul.Image;
import org.zkoss.zul.Include;
import org.zkoss.zul.Label;

/**
 *
 * @author sorasaks
 */
public class PopupCtrl extends IncludePageWsController {
    
    private static final Logger LOG = Logger.getLogger(PopupCtrl.class);
    private Image btnProfilePicture;
    private Label lbBreak1;
    private Label lbBreak2;
    private Button btnProfileName;
    private Button btnProfileSetting;
    private Button btnSignin;
    private Button btnHome;
    private Button btnUS;
    private Button btnTH;
    private static final Base64ConvertorUtils base64 = new Base64ConvertorUtils();
    private static final BaseWebService baseService = WebServiceClient.getBaseService();
    
    public PopupCtrl() throws Exception {
        super();
    }
    
    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp); //To change body of generated methods, choose Tools | Templates.
        initialEvenListener();
        initialComponent();
        initialAuthentification();
        
        LOG.info("::::::::::: pageName :::::::::::::" + pageName);
        LOG.info("::::::::::: locale :::::::::::::" + locale);
        LOG.info("::::::::::: contextPath :::::::::::::" + contextPath);
        LOG.info("::::::::::: realPath :::::::::::::" + realPath);
        LOG.info("::::::::::: amountFormat :::::::::::::" + amountFormat);
        LOG.info("::::::::::: dateFormat :::::::::::::" + dateFormat);
    }
    
    private void initialEvenListener() throws Exception {
        btnHome.addEventListener(Events.ON_CLICK, new btnHomeEventListener());
        btnUS.addEventListener(Events.ON_CLICK, new btnUSEventListener());
        btnTH.addEventListener(Events.ON_CLICK, new btnTHEventListener());
    }
    
    private class btnHomeEventListener extends MainEventWsListener implements EventListener {
        
        public btnHomeEventListener() throws Exception {
            super();
        }
        
        @Override
        public void onEvent(Event t) throws Exception {
            LOG.debug("::::::::::::: Redirect to root path ::::::::::::::");
            Executions.sendRedirect(contextPath);
        }
    }
    
    private class btnUSEventListener extends MainEventWsListener implements EventListener {
        
        public btnUSEventListener() throws Exception {
            super();
        }
        
        @Override
        public void onEvent(Event t) throws Exception {
            Executions.getCurrent().getSession().setAttribute(Attributes.PREFERRED_LOCALE, LocaleType.US);
            initialParameter();
            initialComponent();
            LOG.debug("::::::::::::: Navigator goto prevoius page ::::::::::::::::::");
            
            Include contentInc = (Include) desktop.getAttribute(MainPageWsController.ATTRIBUTE_CONTENT_INCLUDE);
            
            if (contentInc == null || contentInc.getSrc() == null) {
                Executions.sendRedirect(contextPath);
            } else {
                NavigatorUtil nv = new NavigatorUtil(contentInc);
                nv.gotoPage(contentInc.getSrc());
            }
        }
    }
    
    private class btnTHEventListener extends MainEventWsListener implements EventListener {
        
        public btnTHEventListener() throws Exception {
            super();
        }
        
        @Override
        public void onEvent(Event t) throws Exception {
            Executions.getCurrent().getSession().setAttribute(Attributes.PREFERRED_LOCALE, LocaleType.THAI);
            initialParameter();
            initialComponent();
            LOG.debug("::::::::::::: Navigator goto prevoius page ::::::::::::::::::");
            
            Include contentInc = (Include) desktop.getAttribute(MainPageWsController.ATTRIBUTE_CONTENT_INCLUDE);
            
            if (contentInc == null || contentInc.getSrc() == null) {
                Executions.sendRedirect(contextPath);
            } else {
                NavigatorUtil nv = new NavigatorUtil(contentInc);
                nv.gotoPage(contentInc.getSrc());
            }
        }
    }
    
    private void initialComponent() {
        if (locale.equals(LocaleType.US)) {
            btnUS.setVisible(Boolean.FALSE);
            btnTH.setVisible(Boolean.TRUE);
        } else if (locale.equals(LocaleType.THAI)) {
            btnUS.setVisible(Boolean.TRUE);
            btnTH.setVisible(Boolean.FALSE);
        }
    }
    
    private void initialAuthentification() throws Exception {
        if (authentification != null && customerCode != null && authentification) {
            btnSignin.setVisible(Boolean.FALSE);
            lbBreak1.setVisible(Boolean.TRUE);
            lbBreak2.setVisible(Boolean.TRUE);
            btnProfilePicture.setVisible(Boolean.TRUE);
            btnProfileName.setVisible(Boolean.TRUE);
            btnProfileSetting.setVisible(Boolean.TRUE);
            
            ResponseCommon common = (ResponseCommon) base64.stringToGenericObj(baseService.customerCoreGetCustomerByCustomerCode(
                    base64.genericObjToString(customerCode),
                    base64.genericObjToString(Boolean.TRUE)));
            
            Customer customer = (Customer) base64.stringToGenericObj(common.getValue());
            
            btnProfileName.setLabel(customer.getName().trim() + " " + customer.getLastname().trim());
        } else {
            btnSignin.setVisible(Boolean.TRUE);
            lbBreak1.setVisible(Boolean.FALSE);
            lbBreak2.setVisible(Boolean.FALSE);
            btnProfilePicture.setVisible(Boolean.FALSE);
            btnProfileName.setVisible(Boolean.FALSE);
            btnProfileSetting.setVisible(Boolean.FALSE);
        }
    }
}
